#!/usr/local/bin/ruby
# -*- coding: utf-8 -*-

require 'batch_common'

print "\n"
print "batch_site_check\n"
print "start "+Time.now.strftime("%Y-%m-%d %H:%M:%S")+"\n"

#チェック対象URLを取得
def get_check_url(host, check_url)
  if check_url != nil then
    check_url
  else
    "http://" + host + "/"
  end
end

db = getDB()
res = db.query("SELECT id, host, check_url FROM site ORDER BY id DESC")
res.each_hash do |row|
  site_id = row['id']
  url = get_check_url(row['host'], row['check_url'])
  status_code, msec, size = check_site(url)
  sql = sprintf("INSERT INTO check_result (site_id,status_code,msec,size) VALUES(%d,%s,%d,%d)",
                site_id,status_code,msec,size);
  db.query(sql)
end

print "end "+Time.now.strftime("%Y-%m-%d %H:%M:%S")+"\n"
